//
//  ZYCAttendanceEvaluateBottomView.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/30.
//

#import "ZYCAttendanceEvaluateBottomView.h"
#define maxContent 200
#define ZYCContentPlaceholder @"请您给老师提点建议吧~"
#define ZYCContentPlaceholderColor [UIColor colorWithHexString:@"#999999" alpha:1.0]

@interface ZYCAttendanceEvaluateBottomView ()<UITextViewDelegate>

@property (nonatomic, strong) UILabel *scoreLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *line;
@property (nonatomic, strong)UITextView *remarkTextView;
@end
@implementation ZYCAttendanceEvaluateBottomView
- (void)setRemark:(NSString *)remark{
    _remark = remark;
    if (remark.length == 0) {
        _remarkTextView.text = ZYCContentPlaceholder;
        _remarkTextView.textColor = ZYCContentPlaceholderColor;
    }else{
        _remarkTextView.text = remark;
        _remarkTextView.textColor = HEX_RGB(@"#2B302D");
    }

}
- (void)setScore:(NSString *)score{
    _score = score;
    _scoreLabel.text = [NSString stringWithFormat:@"您的评分总分为:%@",score];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = HEX_RGB(kBgWhiteColor);
        [self addSubview:self.scoreLabel];
        [self addSubview:self.titleLabel];
        [self addSubview:self.line];
        [self addSubview:self.remarkTextView];
        
        [self.scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self);
            make.height.mas_equalTo(47.0f);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15.0f);
            make.top.equalTo(self.scoreLabel.mas_bottom).offset(15.0f);
        }];
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15.0f);
            make.right.equalTo(self).offset(-15.0f);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(15.0f);
            make.height.mas_equalTo(1.0f);
        }];
        [self.remarkTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15.0f);
            make.right.equalTo(self).offset(-15.0f);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(16.0f);
            make.height.mas_equalTo(300.0f);
            
        }];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

//时时监听
- (void)textChanged:(NSNotification*)noti{

  //限制输入字数
    [self textViewMaxTextCount:self.remarkTextView AndMaxCount:maxContent];
    
}

/// UITextView字数限制
/// @param textView textView
/// @param maxCount 限制字数
- (void)textViewMaxTextCount:(UITextView *)textView AndMaxCount:(int)maxCount{
    NSString *toBeString =textView.text;
    NSString *lang = [[UIApplication sharedApplication] textInputMode].primaryLanguage;
    if ([lang isEqualToString:@"zh-Hans"]) { // 中文输入
        UITextRange *selectedRange = [textView markedTextRange];
        // 获取高亮部分
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        if (!position) {// 没有高亮选择的字，则对已输入的文字进行字数统计和限制
            if (toBeString.length > maxCount) {
                textView.text = [toBeString substringToIndex:maxCount];
                [MrjToast makeToast:@"字数不能超过200字" makeView:[UIApplication sharedApplication].keyWindow];
            }
        }else{//有高亮选择的字符串，则暂不对文字进行统计和限制
            
        }
    }else{//中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
        if (toBeString.length > maxCount) {
            textView.text = [toBeString substringToIndex:maxCount];
            [MrjToast makeToast:@"字数不能超过200字" makeView:[UIApplication sharedApplication].keyWindow];
        }
    }
}


- (UILabel *)scoreLabel {
    if (!_scoreLabel) {
        _scoreLabel = [UILabel LabelWithString:@"您的评分总分为:0分" Font:kSystemBoldFont(16.0f) titleColor:HEX_RGB(@"#E93C06") textAlignment:ZZTextAlignmentCenter];
        _scoreLabel.backgroundColor = HEX_RGB(@"#F9F9F9");
    }
    return _scoreLabel;
}


- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel LabelWithString:@"课堂亮点与不足" Font:kCustomFont(@"PingFangSC-Light", 16.0f) titleColor:HEX_RGB(@"#2B302D") textAlignment:ZZTextAlignmentLeft];
    }
    return _titleLabel;
}

- (UILabel *)line {
    if (!_line) {
        _line = [[UILabel alloc] init];
        _line.backgroundColor = HEX_RGB(@"#EBEBEB");
    }
    return _line;
}

- (UITextView *)remarkTextView {
    if (!_remarkTextView) {
        _remarkTextView = [[UITextView alloc] init];
        _remarkTextView.delegate = self;
        _remarkTextView.font = kCustomFont(@"PingFangSC-Regular", 16.0f);
        _remarkTextView.text = ZYCContentPlaceholder;
        _remarkTextView.textColor = ZYCContentPlaceholderColor;
        _remarkTextView.scrollEnabled = NO;
    }
    return _remarkTextView;
}



#pragma mark - UITextViewDelegate
-(void) textViewDidEndEditing:(UITextView *)textView{
    if (textView.text.length < 1) {
        textView.text = ZYCContentPlaceholder;
        textView.textColor = ZYCContentPlaceholderColor;
    }else{
        
    }
    
    if (self.saveBlock) {
        self.saveBlock(self.content);
    }
}

-(void) textViewDidBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:ZYCContentPlaceholder]) {
        textView.text = @"";
        textView.textColor = HEX_RGB(@"#2B302D");
    }
}

-(NSString *)content{
    if ([self.remarkTextView.text isEqualToString:ZYCContentPlaceholder]) {
        return nil;
    }
    return self.remarkTextView.text;
}
@end
